/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gblend.largegraph.structure;

import gblend.adjlistgraph.FsgIdEntry;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Vector;

/**
 * The graph that show how the partitioned & border graphs are connected
 * @author HO HOANG HUNG
 */
public class ConnectivityGraph {

    HashMap<Integer, HashMap<Integer, Integer>> connectmap = new HashMap<Integer, HashMap<Integer, Integer>>();
    //Here global id is the key
    HashMap<Integer, Vector<Integer>> globalidmap = new HashMap<Integer, Vector<Integer>>();
    HashMap<Integer, HashSet<Integer>> connectigraph = new HashMap<Integer, HashSet<Integer>>();

    public void addGraphId(int newGid) {
        connectmap.put(newGid, new HashMap<Integer, Integer>());
        connectigraph.put(newGid, new HashSet<Integer>());
    }

    public boolean addNodeMap(int gid, int nodeid, int globalnodeid) {
        Vector<Integer> gbidlist = globalidmap.get(globalnodeid);
        if (gbidlist == null) {
            gbidlist = new Vector<Integer>();
            gbidlist.add(gid);
            gbidlist.add(nodeid);
            globalidmap.put(globalnodeid, gbidlist);
        } else {
            gbidlist.add(gid);
            gbidlist.add(nodeid);
        }
        HashMap<Integer, Integer> map = connectmap.get(gid);
        if (map == null) {
            return false;
        } else {
            map.put(nodeid, globalnodeid);
            return true;
        }
    }

    public void addEdge(int gid1, int gid2) {
        HashSet<Integer> adj = connectigraph.get(gid1);
        adj.add(gid2);
    }

    public boolean isConnected(int gid1, int gid2) {
        HashSet<Integer> adj = connectigraph.get(gid1);
        if (adj.contains(gid2)) {
            return true;
        } else {
            return false;
        }
    }

    public HashSet<Integer> getConnectedGraph(int gid){
        return new HashSet<Integer>(connectigraph.get(gid));
    }

    public boolean isTheSame(int gid1, int localnid1, int gid2, int localnid2) {
        HashMap<Integer, Integer> map1 = connectmap.get(gid1);
        if (map1 == null) {
            return false;
        }
        HashMap<Integer, Integer> map2 = connectmap.get(gid2);
        if (map2 == null) {
            return false;
        }
        Integer global1 = map1.get(localnid1);
        if (global1 == null) {
            return false;
        }
        Integer global2 = map2.get(localnid2);
        if (global2 == null) {
            return false;
        }
        int gi1 = global1;
        int gi2 = global2;
        if (gi1 == gi2) {
            return true;
        } else {
            return false;
        }
    }

    public Vector<Integer> getSameGlobalIdNodes(int gid,int lnodeid){
        HashMap<Integer, Integer> map = connectmap.get(gid);
        if (map == null) {
            return null;
        }
        Integer global = map.get(lnodeid);
        if (global == null) {
            return null;
        }
        return globalidmap.get(global);
    }
}
